'''
Created on Feb 1, 2010

@author: mkiyer
'''

if __name__ == '__main__':
    import sys
    import math
    infile = sys.argv[1]

    minlogfc = 1.0
    minrpkm = 2.0
    rpkm_pad = 0.001
    
    f = open(infile)
    header = f.readline()
    print header
    
    for line in f:
        fields = line.strip().split('\t')

        gsymbol = fields[0]
        gacc = fields[1]
        loc = fields[2]
        
        vcap_h3k4 = int(fields[3])
        vcap_h3k27 = int(fields[4])
        
        lncap_h3k4 = int(fields[5])
        lncap_h3k27 = int(fields[6])
        
        prec_h3k4 = int(fields[7])
        prec_h3k27 = int(fields[8])
        
        vcap_bivalent = vcap_h3k4 and vcap_h3k27
        lncap_bivalent = lncap_h3k4 and lncap_h3k27
        prec_bivalent = prec_h3k4 and prec_h3k27
                
        vcap_rpkm = float(fields[9]) + rpkm_pad
        lncap_rpkm = float(fields[10]) + rpkm_pad
        prec_rpkm = float(fields[11]) + rpkm_pad
        maxrpkm = max(vcap_rpkm, lncap_rpkm, prec_rpkm)
        vcap_logfc = math.log(vcap_rpkm / prec_rpkm)
        lncap_logfc = math.log(lncap_rpkm / prec_rpkm)

        # cancer overexpressed
        if (not (vcap_bivalent and lncap_bivalent)) and prec_bivalent:
            if ((max(vcap_rpkm, lncap_rpkm) > minrpkm) and
                (max(vcap_logfc, lncap_logfc) > minlogfc)):
                print line.strip()
                
        # cancer underexpressed
#        if (vcap_bivalent or lncap_bivalent) and (not prec_bivalent):
#            if ((prec_rpkm > minrpkm) and
#                (min(vcap_logfc, lncap_logfc) < (-minlogfc))):
#                print line.strip()

    f.close()
